package com.testing.open;

import com.alibaba.fastjson.JSONPath;
import com.testing.common.AutoTools;
import com.testing.inter.InterKeyword;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @Classname SpLoginTest
 * @Description 类型说明
 * @Date 2022/9/8 15:48
 * @Created by 特斯汀Roy
 */
public class SpLoginTest {
    public static void main(String[] args) throws IOException {
        //创建httpclient对象用于发包
        CloseableHttpClient client=HttpClients.createDefault();
        //设置http方法和接口url地址
        HttpPost loginPost=new HttpPost("http://localhost:33006/user/login");
        //设置请求体内容
        StringEntity loginParam=new StringEntity("username=roy&password=123456","utf-8");
        //设置请求体content-type
        loginParam.setContentType("application/x-www-form-urlencoded");
        //添加请求体
        loginPost.setEntity(loginParam);
        //执行请求获取返回报文
        CloseableHttpResponse response = client.execute(loginPost);
        //解析报文内容
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "utf-8");
        AutoTools.log.info("返回结果："+result);

        //断言返回结果中json字段为指定信息
        String msg = JSONPath.read(result, "$.msg").toString();
        if("恭喜登录成功".equals(msg)){
            AutoTools.log.info("测试成功");
        }else{
            AutoTools.log.info("测试失败");
        }


        client=HttpClients.createDefault();
        HttpPost loginPostError=new HttpPost("http://localhost:33006/user/login");
        //设置请求体内容
        StringEntity loginParamError=new StringEntity("username=roy&password=1234567","utf-8");
        //设置请求体content-type
        loginParamError.setContentType("application/x-www-form-urlencoded");
        //添加请求体
        loginPostError.setEntity(loginParamError);
        //执行请求获取返回报文
        CloseableHttpResponse responseError = client.execute(loginPostError);
        //解析报文内容
        HttpEntity entityError = responseError.getEntity();
        String resultError = EntityUtils.toString(entityError, "utf-8");
        AutoTools.log.info("返回结果："+resultError);

        //断言返回结果中json字段为指定信息
        String msgError = JSONPath.read(resultError, "$.msg").toString();
        if("用户名密码错误".equals(msgError)){
            AutoTools.log.info("测试成功");
        }else{
            AutoTools.log.info("测试失败");
        }



    }
}
